home *** CD-ROM | disk | FTP | other *** search
- Path: mayne.ugrad.cs.ubc.ca!not-for-mail
- From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
- Newsgroups: comp.lang.c
- Subject: Re: strncpy() ?
- Date: 21 Apr 1996 11:03:40 -0700
- Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
- Message-ID: <4ldt9sINNm1i@mayne.ugrad.cs.ubc.ca>
- References: <4ldkvd$553@venus.compulink.gr>
- NNTP-Posting-Host: mayne.ugrad.cs.ubc.ca
-
- In article <4ldkvd$553@venus.compulink.gr>,
- Costas Vlassis <lonewolf@athena.compulink.gr> wrote:
- >
- > Well I have this question, let's say we have the following :
- >
- >char alfa[40] = "1123";
- >char beta[20];
- >char gama[20];
- >
- >With strncpy(beta,alfa,2) beta = "11" that's fine but HOW can I make
-
- Note that the above strncpy will _not_ null terminate the characters added to
- beta. Unless you are sure that beta[2] contains a zero, the result is not a
- string. Then again, you might not care, but it's just a caveat.
-
- >gama = "23" that is the last 2 characters of alfa or even "3" the last
- >character... Is there a function that takes start and end as strings ?
- >
- >what I want is something like :
- >
- >strncpy2 (beta,alfa,2,4);
- >
- > is this possible ?
-
- strncpy(beta,alfa+2,2);
-
- The expression ``alfa'' has a value that is the pointer to the first character
- of the array alfa, or alfa[0]. You can involve this pointer value in pointer
- arithmetic, as I did above by adding a displacement of 2 to it. You could even
- do:
-
- strncpy(beta+3,alfa+2,2);
-
- which would copy alfa[2] and alfa[3] into beta[3], beta[4].
-